package cn.devices.mapper;

import cn.devices.dto.SelectBorrowDto;
import cn.devices.dto.UserBorrowDto;
import cn.devices.entity.TbBorrow;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 设备出借表 Mapper 接口
 * </p>
 *
 * @author 秋水浮萍
 * @since 2021-02-07
 */
@Repository
public interface TbBorrowMapper {

    @Select("select is_borrow from tb_device where device_code=#{deviceCode}")
    String borrow(String deviceCode);

    @Select("select count(*) from tb_user where user_id=#{userId} and disabled=0")
    int userCount(String userId);

    @Select("select count(*) from tb_device where device_code=#{deviceCode} and disabled=0")
    int count(String deviceCode);

    @Select("select *\n" +
            "from tb_user tu\n" +
            "         join tb_auth_dm tad on tu.dep_code = tad.dep_code\n" +
            "         join tb_device td on tad.type_id = td.type_id\n" +
            "where tu.user_id = #{userId}\n" +
            "  and td.device_code = #{deviceCode}")
    UserBorrowDto userBorrowDtoDm(String userId, String deviceCode);

    @Select("select *\n" +
            "from tb_user tu\n" +
            "         join tb_auth_user tau on tu.user_id = tau.user_id\n" +
            "         join tb_device td on tau.device_code = td.device_code\n" +
            "where tu.user_id = #{userId}\n" +
            "  and td.device_code = #{deviceCode}")
    UserBorrowDto userBorrowDtoSelf(String userId, String deviceCode);

    @Insert("insert into tb_borrow(borrow_id,device_code, user_id, borrow_image, borrow_time, ret_image, borrow_spec)\n" +
            "    VALUE (#{borrowId},#{deviceCode},#{userId},#{borrowImage},#{borrowTime},#{retImage},#{borrowSpec})")
    int tbBorrowAdd(TbBorrow tbBorrow);

    @Select("select user_photo from tb_user where user_id=#{userId}")
    String img(String userId);
    @Update("update tb_device set is_borrow=#{isBorrow},device_status=#{deviceStatus} where device_code=#{deviceCode}")
    int isBorrow(String isBorrow, String deviceCode,String deviceStatus);

    @Update("update tb_borrow set ret_image=#{retImage},ret_time=#{retTime},borrow_spec=#{borrowSpec}" +
            " where user_id=#{userId} and device_code=#{deviceCode} and ret_time is null ")
    int tbBorrowUpdate(TbBorrow tbBorrow);

    @Select("select * from tb_borrow where  device_code=#{deviceCode} and ret_time is null")
    TbBorrow tbBorrowSelect( String deviceCode,String a);

    @Select("<script>" +
            "select td.device_code,\n" +
            "       td.device_name,\n" +
            "       td.device_status,\n" +
            "       td.is_borrow,\n" +
            "       tb.borrow_time,\n" +
            "       tb.ret_time,\n" +
            "       tu.user_name,\n" +
            "       tu.telephone," +
            "       tb.borrow_image,\n" +
            "       tb.ret_image\n" +
            "from tb_borrow tb\n" +
            "         left join tb_device td on td.device_code = tb.device_code\n" +
            "         left join tb_user tu on tb.user_id = tu.user_id\n" +
            "where  td.disabled is not null\n " +
            "<if test='beginTime != null and beginTime!=\"\"'>" +
            " and borrow_time &gt;= #{beginTime}\n" +
            "</if>\n" +
            "<if test='endTime != null and endTime!=\"\"'>" +
            "  and borrow_time &lt;= #{endTime}\n" +
            "</if>\n" +
            "<if test='userName != null and userName!=\"\"'>" +
            "  and tu.user_name like concat('%',#{userName} ,'%')\n" +
            "</if>\n" +
            "<if test='telephone != null and telephone!=\"\"'>" +
            "  and tu.telephone like concat('%',#{telephone} ,'%')\n" +
            "</if>\n" +
            "<if test='deviceCode != null and deviceCode!=\"\"'>" +
            "  and td.device_code like concat('%',#{deviceCode},'%')\n" +
            "</if>\n" +
            "<if test='deviceName != null and deviceName!=\"\"'>" +
            "  and td.device_name like concat('%',#{deviceName},'%')\n" +
            "</if>\n" +
            "<if test='typeId != null and typeId!=\"\"'>" +
            "  and td.type_id = #{typeId}" +
            "</if>" +
            "order by borrow_time\n" +
            "</script>")
    List<SelectBorrowDto> BorrowDtoList(String beginTime, String endTime, String userName, String telephone
            , String deviceCode, String deviceName, String typeId);


    @Select("select * from tb_borrow where user_id=#{userId} order by borrow_time desc ")
    List<TbBorrow> getUserIdBorrow(String userId);
}
